const express = require("express");
const routers = express.Router();
const querystring = require("querystring");
const mysql = require('./pool.js');
routers.post('/reg',(req,res,next)=>{
	/*
	req.on('data',(chunk)=>{
		let data = querystring.parse(chunk.toString());
		res.send(data);
	})
	*/
	let data = req.body;
	mysql.query("insert into xz_user set ?",[data],(err,result)=>{
		if(err){
			next(err);
		}else{
			res.send({code:200,msg:"添加成功"});
		}
		
	})
})

routers.post('/login',(req,res,next)=>{

	let data = req.body;
	if(!data.uname){
		res.send({code:401,msg:"用户名为空"});
		return;
	}
	if(!data.upwd){
		res.send({code:402,msg:"密码为空"});
		return;
	}
	mysql.query('select * from  xz_user where uname = ? and upwd = ?',[data.uname,data.upwd],(err,result)=>{
		if(err){
			next(err);
		}
		if(result.length === 0){
			res.send({code:404,msg:"没有该用户",data:[]});
		}else{
			res.send({code:200,msg:"查找到用户",data:result});
		}
		return;
		
	})
})



module.exports = routers;
